package main.function;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtilities implements Runnable {
	private OutputStream out;
	private InputStream in;

	public boolean copyFile(File source, File target) {
		try {
			this.in = new FileInputStream(source);
			this.out = new FileOutputStream(target);

			Thread t = new Thread(this);
			t.start();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void run() {
		if (in == null || out == null) {
			System.err
					.println("Error in construct. -- " + getClass().getName());
			return;
		}
		byte[] buffer = new byte[1024];
		int bytesRead;
		try {
			bytesRead = in.read(buffer);
			while (bytesRead >= 0) {
				out.write(buffer, 0, bytesRead);
				bytesRead = in.read(buffer);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
